package leetcode_周赛._2022._202205;

import java.util.Arrays;

/**
 * @author yzh
 * @version 1.0
 * @date 2022/5/22 10:46
 */
public class _22 {
    public static void main(String[] args) {
        new _22().minimumLines(new int[][]{{36, 9}, {17, 93}, {34, 4}, {30, 11}, {11, 41}, {53, 36}, {5, 92}, {81, 92}, {28, 36}, {3, 45}, {72, 33}, {64, 1}, {4, 70}, {16, 73}, {99, 20}, {49, 33}, {47, 74}, {83, 91}});
    }

    public int minimumLines(int[][] stockPrices) {
        int n = stockPrices.length;
        if (n == 0) return 1;
        Arrays.sort(stockPrices, (a, b) -> {
            if (a[0] != b[0]) return a[0] - b[0];
            return a[1] - b[1];
        });
        int ans = 0;
        for (int i = 0; i < n - 1; ) {
            int subd = stockPrices[i + 1][0] - stockPrices[i][0], subp = stockPrices[i + 1][1] - stockPrices[i][1];
            if (stockPrices[i][1] < stockPrices[i + 1][1]) {
                ++i;
                while (i < n - 1 && stockPrices[i][1] < stockPrices[i + 1][1] &&
                        (stockPrices[i + 1][0] - stockPrices[i][0]) * subp == (stockPrices[i + 1][1] - stockPrices[i][1]) * subd)
                    ++i;
                ++ans;
            } else if (stockPrices[i][1] == stockPrices[i + 1][1]) {
                while (i < n - 1 && stockPrices[i][1] == stockPrices[i + 1][1] &&
                        (stockPrices[i + 1][0] - stockPrices[i][0]) * subp == (stockPrices[i + 1][1] - stockPrices[i][1]) * subd)
                    ++i;
                ++ans;
            } else {
                while (i < n - 1 && stockPrices[i][1] > stockPrices[i + 1][1] &&
                (stockPrices[i + 1][0] - stockPrices[i][0]) * subp == (stockPrices[i + 1][1] - stockPrices[i][1]) * subd)
                    ++i;
                ++ans;
            }
        }
        return ans;
    }

}
